<?php
/**
 * Klasse zum erstellen eines Navbar Eintrags
 *
 * @author		Joachim Ruf, Loresoft Software (http://www.loresoft.de)
 * @license		New BSD License
 * @version		Class for Bootstrap v2.3.2
 *
 * */


Class Bootstrap_Navbar_Entry
{
	private $arrList			= array();
	private $active				= false;
	private $content			= NULL;
	private $arrParam			= NULL;
	private $isActive			= false;
	private $useTag				= 'a'; // Angabe, ob ein a-Tag verwendet werden soll oder nicht


	/**
	 * @param string $_size		: breite {1-12}
	 * @param string $_content	: Auszugebender Content
	 * */
	function __construct ($_content = NULL, array $_arrParam = array(), $_useTag = 'a') {
		if (!isset($_arrParam['href'])) {
			if (isset($_arrParam['style'])) {
				$_arrParam['style']	.= 'cursor: pointer;';
			} else {
				$_arrParam['style']	= 'cursor: pointer;';
			}
		}

		$this->content	= $_content;
		$this->arrParam	= $_arrParam;
		$this->useTag	= $_useTag;
	}


	private function getParameter ($_class, array $_arrParam) {
		$result	= NULL;

		if (isset($_arrParam['class'])) { // Class-Parameter verbinden
			$_arrParam['class']	= $_arrParam['class'].' '.$_class;
		} else {
			$_arrParam['class']	= $_class;
		}

		foreach ($_arrParam as $key => $val) {
			$result	.= ' '.$key.'="'.$val.'"';
		}

		return $result;
	}


	public function setActive($_enable = true) {
		$this->active	= $_enable;
	}


	public function isActive() {
		return $this->isActive;
	}


	/**
	 * Liefert den Content zurueck
	 * @return int
	 * */
	public function getContent() {
		return $this->content;
	}


	/**
	 * Erstellt eine Subliste
	 * @param string $_content	: Auszugebender Content
	 * @param array	$_arrParam	: array('href' => 'url', 'title' => 'Title-Text', 'rel' => 'follow|nofollow|..')
	 * @return Bootstrap_Navbar_Entry
	 * */
	public function addSubentry($_content, array $_arrParam = array()) {
		return $this->arrList[]	= new Bootstrap_Navbar_Entry($_content, $_arrParam);
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param string $_content	: Auszugebender Content
	 * @param array	$_arrParam	: array('href' => 'url', 'title' => 'Title-Text', 'rel' => 'follow|nofollow|..')
	 * @return Bootstrap_Navbar_Entry
	 * */
	public function addDivider (array $_arrParam = array()) {
		return $this->arrList[] = new Bootstrap_Navbar_Divider(array('class' => 'divider'));
	}


	private function isCurrentUrl () {
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
		$urlHost	= $protocol.$_SERVER['HTTP_HOST'];
		$urlFull	= $urlHost.$_SERVER['REQUEST_URI'];

		if (!isset($this->arrParam['href'])) {
			return false;
		}
		if ($this->arrParam['href'] == '#') {
			return false;
		}

		$url		= $this->arrParam['href'];
		$arrUrl		= parse_url($url);
		$arrFull	= parse_url($urlFull);
		if (!isset($arrUrl['path']) || !isset($arrFull['path'])) {
			return false;
		}

		$arrUrl['path']		= ltrim($arrUrl['path'], '/');
		$arrFull['path']	= ltrim($arrFull['path'], '/');
		if ($arrUrl['path'] != $arrFull['path']) { // @notive: Could make problems on localhost
			return false;
		}

		$this->isActive	= true;
		return true;
	}


	/**
	 * @desc Gibt den Datensatz aus.
	 * @return string
	 * */
	public function display($_autoActive = false, $_backwardReversing = false) {
		$result	= NULL;

		$active	= ($this->active === true) ? 'active' : '';
		if ($_autoActive === true && $this->isCurrentUrl()) {
			$active	= 'active';
		}

		if (count($this->arrList)) {
			/* @var $oObject Bootstrap_Navbar_Entry */
			foreach ($this->arrList as $Key => $oObject) {
				$result	.= $oObject->display($_autoActive, $_backwardReversing);
				if ($_backwardReversing === true && $oObject->isActive()) {
					$active	= 'active';
				}
			}

			return '<li class="dropdown '.$active.'">
						<'.$this->useTag.' '.$this->getParameter(''.$active, $this->arrParam).'>'.$this->content.' <b class="caret"></b></'.$this->useTag.'>
						<ul class="dropdown-menu">
							'.$result.'
						</ul>
					</li>';
		} else {
			return '<li class="'.$active.'"><'.$this->useTag.' '.$this->getParameter('', $this->arrParam).'>'.$this->content.'</'.$this->useTag.'></li>';
		}

	}


}


?>